package com.wsb.wetchat.controller;

import com.wsb.wetchat.service.ThreadPoolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @author wang shaobo
 * @date 2022/6/29  15:53
 */
@RestController
public class ThreadPoolController {

    @Autowired
    ThreadPoolService threadPoolService;

    @GetMapping("/threadpool/async")
    public String async(@RequestParam(value = "str") String str) throws InterruptedException {
        String name = Thread.currentThread().getName();
        System.out.println(name + "开始执行同步任务：参数:" + str);

        threadPoolService.exectAsync(str);
//        TimeUnit.SECONDS.sleep(3);
        return "异步任务"+str;

    }

    @GetMapping("/threadpool/sync")
    public String sync(@RequestParam(value = "str") String str) throws InterruptedException {
        String name = Thread.currentThread().getName();
        System.out.println(name + "开始执行同步任务：参数:" + str);

        threadPoolService.exectSync(str);
        TimeUnit.SECONDS.sleep(3);
        return "执行同步任务："+str;

    }


}
